/*
	Author: Genesis

	Description:
		Plants a mine

	Parameter(s):
		0: OBJECT - Unit to plant a mine
		1: ARRAY - ???

	Returns:
		NOTHING
		
	k1 edit 2021.9.12
*/

params ["_unit", "_mineArray"];

if (lifeState _unit != "HEALTHY" || {isPlayer _unit}) exitWith {};

if (VCM_Debug) then {systemchat format ["VCOM: %1 PLACING MINE", _unit]};

private _mineType = _mineArray select 0;

//Let's see if we can place a scripted version of the item.
private _testName = _mineType + "_scripted";
private _testMine = _testName createVehiclelocal [0,0,0];
if (!isNull _testMine) then {_mineType = _testName};

private _magazineName = _mineArray select 1;

if (_mineArray isEqualTo []) exitWith {};

//systemchat format ["I %1",_unit];
private _nearestEnemy = _unit call VCM_fnc_ClstEmy;
if (_nearestEnemy isEqualTo [] || {isNil "_nearestEnemy"} || {!alive _unit}) exitWith {};

private _mine = "";

if (_nearestEnemy distance2D _unit < 100) then [{
	//_mine = createMine [_mineType,getposATL _unit, [], 2];
	private _mPos = _unit modeltoworld [0,1,.05];
	_mine = _mineType createVehicle _mPos;
	_mine setDir ([_mine, _nearestEnemy] call BIS_fnc_dirTo);
	_mine setpos _mPos;
	_mine setposATL (getposATL _mine);
	_unit removeMagazine _magazineName;
	[_unit,"AinvPknlMstpSnonWnonDnon_Putdown_AmovPknlMstpSnonWnonDnon"] remoteExec ["Vcm_PMN",0];
},{
	_nearRoads = _unit nearRoads 50;
	if (count _nearRoads > 0) then [{
		private _closestRoad = [_nearRoads,_unit,true,"2"] call VCM_fnc_ClstObj;
		doStop _unit;
		_unit doMove (getpos _closestRoad);
		waitUntil {!alive _unit || {_unit distance2D _closestRoad < 7}};
		if (!alive _unit) exitWith {}; //
		private _mPos = _unit modeltoworld [0,1,.05];
		_mine = _mineType createVehicle _mPos;
		_mine setDir ([_mine, _nearestEnemy] call BIS_fnc_dirTo);
		_mine setpos _mPos;
		_mine setposATL (getposATL _mine);
		_unit removeMagazine _magazineName;
		//_mine = createMine [_mineType,getposATL _closestRoad, [], 3];
		[_unit,"AinvPknlMstpSnonWnonDnon_Putdown_AmovPknlMstpSnonWnonDnon"] remoteExec ["Vcm_PMN",0];
	},{
		//_mine = createMine [_mineType,getposATL _unit, [], 3];
		private _mPos = _unit modeltoworld [0,1,.05];
		_mine = _mineType createVehicle _mPos;
		_mine setDir ([_mine, _nearestEnemy] call BIS_fnc_dirTo);
		_mine setpos _mPos;
		_mine setposATL (getposATL _mine);
		_unit removeMagazine _magazineName;
		[_unit,"AinvPknlMstpSnonWnonDnon_Putdown_AmovPknlMstpSnonWnonDnon"] remoteExec ["Vcm_PMN",0];
	}];
}];

_unitSide = side _unit;


if (_mine isEqualTo "") exitWith {};

VCOM_mineArray pushBack [_mine,_unitSide];
[_Mine, false] remoteExecCall ["enableSimulationGlobal",2];
//_mine spawn {sleep 8; [_this, true] remoteExecCall ["enableSimulationGlobal",2]}; //k1

/*
[_mine,_unitSide] spawn 
{
	params ["_Mine","_unitSide"];
	private _NotSafe = true;
	[_Mine, false] remoteExecCall ["enableSimulationGlobal",2];
	waitUntil {
		private _Array1 = (allUnits select {!(side _x isEqualTo _unitSide)});
		private _ClosestEnemy = [0,0,0];
		_ClosestEnemy = [_Array1,_Mine,true,"2"] call VCM_fnc_ClstObj;
		if (_ClosestEnemy distance _Mine < 2.5) then {_NotSafe = false;};
		sleep 0.1;	
		(!(alive _mine) || {!(_NotSafe)})
	};
	[_Mine, true] remoteExecCall ["enableSimulationGlobal",2];
	sleep 0.25;
	_Mine setdamage 1;
};